package com.example.demo.websocket.client;

import org.java_websocket.WebSocket;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public class MyWebSocketServer extends WebSocketServer {


    public MyWebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts, Collection<WebSocket> connectionscontainer) {
        super(address, decodercount, drafts, connectionscontainer);
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        System.out.println("===============================================================\n连接成功");
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        System.out.println("关闭连接");
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        System.out.println("接收到消息：" + message);
        conn.send(message);
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        System.out.println("出现错误信息--->");
        ex.printStackTrace();
    }

    @Override
    public void onStart() {
        System.out.println("开始");
    }

    static         String                host                 = "127.0.0.1";
    static         int                   port                 = 81;
    private static InetSocketAddress     address              = new InetSocketAddress(host, port);
    private static int                   decoderCount         = WebSocketServer.DECODERS;
    private static List<Draft>           drafts               = new ArrayList<>(0);
    private static Collection<WebSocket> connectionsContainer = new HashSet<>(0);

    public static void main(String[] args) {


        MyWebSocketServer myWebSocketServer = new MyWebSocketServer(address, decoderCount, drafts, connectionsContainer);


        myWebSocketServer.start();
    }

}